package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.DevSportData;
import com.bdmc.device.domain.bo.DevSportDataBo;
import com.bdmc.device.domain.vo.DevSportDataVo;
import com.bdmc.device.mapper.DevSportDataMapper;
import com.bdmc.device.service.IDevSportDataService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 手环运动数据Service业务层处理
 *
 * @author bdmc
 * @date 2023-07-31
 */
@Service
public class DevSportDataServiceImpl extends ServicePlusImpl<DevSportDataMapper, DevSportData, DevSportDataVo> implements IDevSportDataService {

    @Override
    public DevSportDataVo queryById(Long sportId){
        return getVoById(sportId);
    }

    @Override
    public TableDataInfo<DevSportDataVo> queryPageList(DevSportDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevSportData> lqw = buildQueryWrapper(bo);
        Page<DevSportDataVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevSportDataVo> mySelectPage(DevSportDataBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevSportDataVo> queryList(DevSportDataBo bo) {
        LambdaQueryWrapper<DevSportData> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevSportData> buildQueryWrapper(DevSportDataBo bo) {
        LambdaQueryWrapper<DevSportData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getImei()), DevSportData::getImei, bo.getImei());
        lqw.eq(StringUtils.isNotBlank(bo.getSportstep()), DevSportData::getSportstep, bo.getSportstep());
        lqw.eq(bo.getSportdistance() != null, DevSportData::getSportdistance, bo.getSportdistance());
        lqw.eq(bo.getSportcalorie() != null, DevSportData::getSportcalorie, bo.getSportcalorie());
        lqw.eq(bo.getSporttime() != null, DevSportData::getSporttime, bo.getSporttime());
        lqw.eq(bo.getDataTime() != null, DevSportData::getDataTime, bo.getDataTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevSportDataBo bo) {
        DevSportData add = BeanUtil.toBean(bo, DevSportData.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setSportId(add.getSportId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevSportDataBo bo) {
        DevSportData update = BeanUtil.toBean(bo, DevSportData.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevSportData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public DevSportDataVo getVoByImei(String imei) {
        LambdaQueryWrapper<DevSportData> lqw = Wrappers.lambdaQuery();
        lqw.eq(DevSportData::getImei,imei).orderByDesc(DevSportData::getSportId).last(" limit 1");
        return getVoOne(lqw);
    }
}
